﻿using System.Data;
using SqlSugar;
using StoreAPI.Models;

namespace StoreAPI.Services.ServerImpls
{
    public class InventoryService : IInventoryService
    {
        private  ISqlSugarClient _db;

        public InventoryService(ISqlSugarClient db)
        {
            _db = db;
        }
        //获取最大ID
        public int GetMaxId()
        {
            return _db.Queryable<Inventory>()
               .Max(x => x.InventoryId);
        }
        //判断是否存在
        public bool Exists(int model)
        {
            return _db.Queryable<Inventory>()
               .Any(x => x.InventoryId == model);
        }
        //注册
        public bool Add(Inventory model)
        {
            return _db.Insertable<Inventory>(model).ExecuteCommand() > 0;
        }
        //更新
        public bool Update(Inventory model)
        {
            return _db.Updateable<Inventory>(model).ExecuteCommand() > 0;
        }
        //删除
        public bool Delete(int primaryKey)
        {
            return _db.Deleteable<Inventory>(primaryKey).ExecuteCommand() > 0;
        }
        //批量删除
        public bool DeleteList(int[] primaryKeys)
        {
            return _db.Deleteable<Inventory>().In(primaryKeys).ExecuteCommand() > 0;
        }
        //查询单个
        public Inventory GetModel(int primaryKey)
        {
            return _db.Queryable<Inventory>()
               .InSingle(primaryKey);
        }
        //查询一行数据转Inventory模型
        public Inventory DataRowToModel(DataRow row)
        {
            

            Inventory inventory = new Inventory();
            if (row != null)
            {
                inventory.InventoryId = Convert.ToInt32(row["InventoryId"]);
                inventory.ProductId = Convert.ToInt32(row["ProductId"]);
                inventory.StoreId = Convert.ToInt32(row["StoreId"]);
                inventory.StoreAreaId = Convert.ToInt32(row["StoreAreaId"]);
                inventory.Amount = Convert.ToInt32(row["Amount"]);
                inventory.Remark = row["Remark"].ToString()!;
                inventory.Status = Convert.ToInt32(row["Status"]);
                inventory.CreateUserId = Convert.ToInt32(row["CreateUserId"]);
                inventory.CreateTime = Convert.ToDateTime(row["CreateTime"]);
                inventory.LastUpdateUserId = Convert.ToInt32(row["LastUpdateUserId"]);
                inventory.LastUpdateTime = Convert.ToDateTime(row["LastUpdateTime"]);

            }
            return inventory;

        }
        //查询所有
        public List<Inventory> GetListAll(Expressionable<Inventory> exp)
        {
            return _db.Queryable<Inventory>()
               .Where(exp.ToExpression())
               .ToList();
        }
        //获得前几行数据
        public List<Inventory> GetList(Expressionable<Inventory> exp, int Top)
        {
            return _db.Queryable<Inventory>()
               .Where(exp.ToExpression())
               .Take(Top)
               .ToList();
        }
        //获取记录数
        public int GetRecordCount(Expressionable<Inventory> exp)
        {
            return _db.Queryable<Inventory>()
               .Where(exp.ToExpression())
               .Count();
        }
        //分页查询
        public List<Inventory> GetListByPage(Expressionable<Inventory> exp, out int totalPage, int pageIndex = 1, int pageSize = 10)
        {
            totalPage = 0;
            var result = _db.Queryable<Inventory>()
               .Where(exp.ToExpression())
               .ToPageList(pageIndex, pageSize, ref totalPage);
            return result;
        }
    }
}
